import json
import math
import datetime
import uuid
from typing import List, Dict

import requests

from django.conf import settings

from data.models import Purchase
from utils.encoder import JSONEncoder


# 采集贵州省数据
def sync_guizhou_purchases() -> None:
    print('采集贵州省采购数据任务开始...')
    days = settings.SYNC_CONFIG.get('DAYS', 3) - 1
    list_api: str = settings.SYNC_CONFIG.get('GUIZHOU', {}).get('LIST_API', '')
    detail_api: str = settings.SYNC_CONFIG.get('GUIZHOU', {}).get('DETAIL_API', '')
    site_url: str = settings.SYNC_CONFIG.get('GUIZHOU', {}).get('SITE_URL', '')
    params: List[str] = settings.SYNC_CONFIG.get('GUIZHOU', {}).get('params', [])
    keys: List[str] = settings.SYNC_CONFIG.get('KEYS', [])
    page_size: int = 15
    print(f'每次采集{page_size}条')
    for param in params:
        print(f'当前参数: {param}')
        now: datetime.datetime = datetime.datetime.now()
        start_date: str = (now + datetime.timedelta(days=-days)).strftime('%Y-%m-%d')
        end_date: str = now.strftime('%Y-%m-%d')
        now_time: int = int(now.timestamp() * 1000)
        x_request_id: str = str(uuid.uuid4()).replace('-', '')
        total_page: int = get_total_page(list_api, x_request_id, param, now_time, start_date, end_date, page_size)
        print(f'共采集: {total_page}次')
        if total_page:
            for page in range(1, total_page):
                print(f'第{page}次...')
                res = requests.post(
                    url=list_api,
                    headers={
                        'Accept': 'application/json',
                        'Content-Type': 'application/json;charset=UTF-8',
                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
                        'X-Request-Id': x_request_id,
                        'Server': 'core'
                    },
                    data=json.dumps({
                        'categoryCode': param,
                        'pageNo': page,
                        'pageSize': page_size,
                        '_t': now,
                        'publishDateBegin': start_date,
                        "publishDateEnd": end_date
                    }, cls=JSONEncoder),
                )
                if res.status_code == 200:
                    data: Dict = res.json()
                    if data.get('success', False):
                        data1: List[Dict] = data.get('result', {}).get('data', {}).get('data', [])
                        for d in data1:
                            article_id: str = d.get('articleId', '')
                            x_request_id: str = str(uuid.uuid4()).replace('-', '')
                            now_time: int = int(datetime.datetime.now().timestamp() * 1000)
                            print(f'采集详情参数: 文章ID: {article_id}, 请求ID: {x_request_id}, 当前时间戳: {now_time}')
                            res1 = requests.get(
                                url=detail_api,
                                headers={
                                    'Accept': 'application/json',
                                    'Content-Type': 'application/json;charset=UTF-8',
                                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
                                    'X-Request-Id': x_request_id,
                                    'Server': 'core'
                                },
                                params={
                                    'articleId': article_id,
                                    'timestamp': now_time
                                }
                            )
                            if res.status_code == 200:
                                detail_data: Dict = res1.json()
                                if detail_data.get('success', False):
                                    detail_data1: Dict = detail_data.get('result', {}).get('data', {})
                                    content: str = detail_data1.get('content', '')
                                    for kw in keys:
                                        if content.find(kw) > -1:
                                            print(f'匹配到关键词: {kw}')
                                            default_data: Dict = {
                                                'province': '贵州省',
                                                'keyword': kw,
                                                'author': detail_data1.get('author', ''),
                                                'budget_price': d.get('budgetPrice', ''),
                                                'district_code': d.get('districtCode', ''),
                                                'district_name': d.get('districtName', ''),
                                                'path_name': d.get('pathName', ''),
                                                'procurement_method': d.get('procurementMethod', ''),
                                                'project_code': detail_data1.get('projectCode', ''),
                                                'project_name': detail_data1.get('projectName', ''),
                                                'publish_date': datetime.datetime.fromtimestamp(
                                                    detail_data1.get('publishDate', 0) / 1000).strftime('%Y-%m-%d'),
                                                'title': detail_data1.get('title', ''),
                                                'content': detail_data1.get('content', ''),
                                                'url': site_url + '&articleId={}'.format(article_id),
                                                'expire_flag': detail_data1.get('expireFlag', 0),
                                                'expire_at': datetime.datetime.fromtimestamp(
                                                    detail_data1.get('expireAt', 0) / 1000).strftime('%Y-%m-%d'),
                                                'implementation_date': detail_data1.get('implementationDate', None),
                                                'is_gov_purchase': detail_data1.get('isGovPurchase', 0)
                                            }
                                            p, created = Purchase.objects.select_related().get_or_create(
                                                id=article_id,
                                                defaults=default_data
                                            )
                                            if not created:
                                                Purchase.objects.filter(id=article_id).update(**default_data)
        print('采集完成.')


# 获取分页参数
def get_total_page(url: str, x_request_id: str, param: str, now: int, start_date: str, end_date: str, page_size: int) -> int:
    res = requests.post(
        url=url,
        headers={
            'Accept': 'application/json',
            'Content-Type': 'application/json;charset=UTF-8',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
            'X-Request-Id': x_request_id,
            'Server': 'core'
        },
        data=json.dumps({
            'categoryCode': param,
            'pageNo': 1,
            'pageSize': page_size,
            'publishDateBegin': start_date,
            'publishDateEnd': end_date,
            '_t': now
        }, cls=JSONEncoder)
    )
    if res.status_code == 200:
        data: Dict = res.json()
        if data.get('success', False):
            total: int = data.get('result', {}).get('data', {}).get('total', 0)
            return math.ceil(total / page_size)
    return 0